
import { PlusOutlined } from '@ant-design/icons';
import { Button, message, Input, Drawer } from 'antd';
import React, { useState, useRef, useEffect } from 'react';
import {useIntl, FormattedMessage, history} from 'umi';
import { PageContainer, FooterToolbar } from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import {ProFormText, ProFormTextArea } from '@ant-design/pro-form';
import {resolve, getAllDos, getRepoAndRegs, getAllUsers} from '@/services/doa/irs'
import ReactJson from 'react-json-view'
import ProForm from '@ant-design/pro-form'
import {deleteDo} from "@/services/doa/repo";

const IdAdmin = () => {


  const doRef = useRef();
  const repoRef = useRef();
  const [doInfo,setDoInfo] = useState([])
  const [repoInfo,setRepoInfo] = useState([])

  useEffect(()=>{
    getAllDos().then((res)=>{
      if(res?.success){
        setDoInfo(res?.data?.map((item)=>{
          const digitalObject = JSON.parse(item)
          return {doi:digitalObject?.identifier, repo:digitalObject?.handleValues?.repository, owner:digitalObject?.handleValues?.owner}

        }))
      }
    })

    getRepoAndRegs().then((res)=>{
      if(res?.success){
        setRepoInfo(res?.data?.map((item)=>{
          const repo = JSON.parse(item)
          return {doi:repo?.identifier, type:repo?.handleValues?.type, owner:repo?.handleValues?.owner}
        }))
      }
    })
  },[])

  useEffect(()=>{
    doRef.current.reloadAndRest()
  },[doInfo])

  useEffect(()=>{
    repoRef.current.reloadAndRest()
  },[repoInfo])

  /**
   * @en-US International configuration
   * @zh-CN 国际化配置
   * */

  const intl = useIntl();

  const doColumns = [
    {
      title: '序号',
      dataIndex: 'index',
      valueType: 'indexBorder',
    },
    {
      dataIndex: 'doi',
      title: '标识',
      search: false,
    },
    {
      dataIndex: 'repo',
      title: '所在repo',
      search: false,
    },
    {
      dataIndex: 'owner',
      title: '所有者',
      search: false,
    },
    {
      title: '操作',
      valueType: 'option',
      width: 250,
      render: (text, record, _, action) => [
        <a
          key="detail"
          onClick={() => {
            history.push({pathname: "/irs/resolve", query: {"doi": record.doi}})
          }}
        >
          详情
        </a>,
      ],
    },
  ];

  const repoColumns = [
    {
      title: '序号',
      dataIndex: 'index',
      valueType: 'indexBorder',
    },
    {
      dataIndex: 'doi',
      title: '标识',
      search: false,
    },
    {
      dataIndex: 'type',
      title: '类型',
      search: false,
    },
    {
      dataIndex: 'owner',
      title: '所有者',
      search: false,
    },
    {
      title: '操作',
      valueType: 'option',
      width: 250,
      render: (text, record, _, action) => [
        <a
          key="detail"
          onClick={() => {
            history.push({pathname: "/irs/resolve", query: {"doi": record.doi}})
          }}
        >
          详情
        </a>,
      ],
    },
  ];

  return (
    <PageContainer>
      <ProTable
        headerTitle={intl.formatMessage({
          id: 'pages.irsAdmin.doTable.title',
          defaultMessage: '数字对象列表',
        })}
        style={{"margin-bottom": "20px"}}
        actionRef={doRef}
        search={false}
        rowKey="key"
        options={{
          search: true,
        }}
        request={async (params, sort, filter) => {
         //{current: 1, pageSize: 20, keyword: "21221"}
          console.log(params,sort,filter)
          return Promise.resolve({
            data: doInfo,
            success: true,
          });
        }}
        columns={doColumns}
      />

      <ProTable
        headerTitle={intl.formatMessage({
          id: 'pages.irsAdmin.repoTable.title',
          defaultMessage: '基础设施列表',
        })}
        actionRef={repoRef}
        search={false}
        rowKey="key"
        options={{
          search: true,
        }}
        request={async (params, sort, filter) => {
          return Promise.resolve({
            data: repoInfo,
            success: true,
          });
        }}
        columns={repoColumns}
      />


    </PageContainer>
  );
};

export default IdAdmin;

